import os
import time

from PIL import Image, ImageDraw

from ocr_loc import OCR


def draw_coordinates_on_image(image_path, coordinates):
    """
    绘制坐标点
    """
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    point_size = 5
    for coord in coordinates:
        draw.ellipse(
            (
                coord[0] - point_size,
                coord[1] - point_size,
                coord[0] + point_size,
                coord[1] + point_size,
            ),
            fill="red",
        )
    output_image_path = image_path + "_with_point.jpg"
    image.save(output_image_path)
    return output_image_path


root = os.path.dirname(os.path.abspath(__file__))
img_path = os.path.join(root, "test.jpg")


print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())))
ocr = OCR()
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())))
r = ocr.ocr(img_path)
for k, v in r.items():
    print(k, v)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())))


center_list = [v[0] for v in r.values()]
draw_coordinates_on_image(img_path, center_list)
